package com.njbdqn.qiy_book.bookcontroller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.njbdqn.commons.services.OutputService;

import com.njbdqn.qiy_book.service.ListService;
import com.njbdqn.qiy_book.vo.BookNoNameVO;
import com.njbdqn.qiy_book.vo.BooknameVO;
import com.njbdqn.qiy_book.vo.TypeBookVO;

/**
 * Servlet implementation class BookList
 */
public class BookList extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String base = "";
		List<TypeBookVO> tbvo = new ArrayList<TypeBookVO>();
		for (int i = 0; i < 3; i++) {
			if (i == 0) {
				base = "totalbuy";
			} else if (i == 1) {
				base = "bookid";
			} else {
				base = "totalclick";
			}

			ListService ls = new ListService();
			// List<TypenameVO> tv = ls.findTypeByBase(base);
			List<BooknameVO> bv = ls.findBookByBase(base);
			BookNoNameVO bnv = ls.findBookNoNameByBase(base).get(0);
			TypeBookVO tbv = new TypeBookVO();
			// tbv.setType(tv);
			tbv.setBook(bv);
			try {
				BeanUtils.copyProperties(tbv, bnv);
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tbvo.add(tbv);
		}
		OutputService.out(response, tbvo);
	}

}
